﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
using System.Globalization;
using System.Resources;
using DataAccess.DataObject;
using EnmineKaitlin.Common;
using System.Collections.Generic;

namespace EnmineKaitlin.admin
{
    public partial class edit_category : Common.PageBase
    {
        int CatID = 0;
        const string module = "manage-post";
        string lang = "vn";
        int catparent = 0;
        int currentpage = 1;
        const int pagesize = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write(Request.Params["cat_type"]);
            if (CheckPermission(module))
            {
                if (!IsPostBack)
                {                    
                    if (Request.Params["cat"] != null)
                    {
                        CatID = int.Parse(Request.Params["cat"].ToString());
                    }
                    if (CatID > 0)
                    {
                        BindDetailCat(CatID);
                    }
                    else
                    {

                    }
                    GetParam();
                    BindCategory();
                }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }

        protected void ddlLanguage_SelectedIndexChange(object sender, EventArgs e)
        {
            DropDowListCategory.LangID = ddlLanguage.SelectedValue;
            DropDowListCategory.DataBind();
        }
        protected void ddlLanguageFilter_SelectedIndexChange(object sender, EventArgs e)
        {
            lang = ddlLanguageFilter.SelectedValue;
            ucddlcCategoryFilter.LangID = lang;
            ucddlcCategoryFilter.DataBind();
            BindCategory();
        }
        protected void BindDetailCat(int CatID)
        {
            IList<OCategory> lstCategory = _BCategory.Get(CatID,"");
            ddlLanguage.SelectedValue = lstCategory[0].LangID;
            txtCatName.Text = lstCategory[0].CatName;
            DropDowListCategory.LangID = lstCategory[0].LangID; 
            DropDowListCategory.SelectedValue = lstCategory[0].CatParent.ToString();
            DropDowListCategory.DataBind();
            txtDescription.Text = lstCategory[0].Description;
            hdfFeaturedImage.Value = lstCategory[0].FeaturedImage;
            imgFeaturedImage.ImageUrl = lstCategory[0].FeaturedImage;
            btnAddCat.Text = "Cập nhật";
            btnAddCat.CommandName = "UpdateCat";
        }
        /// <summary>
        /// Thêm mới chuyên mục
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddCat_Click(Object sender, CommandEventArgs e)
        {
            string cat_type = "category";
            if(Request.Params["cat_type"] != null){
                cat_type = Request.Params["cat_type"].ToString();
            }
            switch (e.CommandName)
            { 
                case "AddCat":
                    _OCategory.CatName = txtCatName.Text;
                    _OCategory.CatSlug = "";
                    _OCategory.CatParent = int.Parse(DropDowListCategory.SelectedValue);
                    _OCategory.Description = txtDescription.Text;
                    _OCategory.FeaturedImage = hdfFeaturedImage.Value;
                    _OCategory.LangID = ddlLanguage.SelectedValue;
                    _OCategory.CatType = cat_type;
                    if (_BCategory.Add(_OCategory))
                    {
                        txtCatName.Text = "";
                        txtDescription.Text = "";
                        hdfFeaturedImage.Value = "";
                        imgFeaturedImage.ImageUrl = "";                        
                        BindCategory();
                    }
                    break;
                case "UpdateCat":
                    if (Request.Params["cat"] != null)
                    {
                        CatID = int.Parse(Request.Params["cat"].ToString()); 
                    }
                    if (CatID > 0)
                    {
                        _OCategory.CatID = CatID;
                        _OCategory.CatName = txtCatName.Text;
                        _OCategory.CatSlug = "";
                        _OCategory.CatParent = int.Parse(DropDowListCategory.SelectedValue);
                        _OCategory.Description = txtDescription.Text;
                        _OCategory.FeaturedImage = hdfFeaturedImage.Value;
                        _OCategory.LangID = ddlLanguage.SelectedValue;
                        if (_BCategory.Update(_OCategory))
                        {                            
                            BindCategory();
                            BindDetailCat(CatID);
                        }
                    }
                    break;
            }            
        }
        protected void GetParam()
        {
            if (Request["lang"] != null && Request["lang"].ToString() != "")
            {
                lang = Request["lang"];
                ddlLanguageFilter.SelectedValue = lang;
                ddlLanguageFilter.DataBind();
            }
            if (Request["catparent"] != null && Request["catparent"].ToString() != "")
            {
                try
                {
                    catparent = int.Parse(Request["catparent"].ToString());
                }
                catch (Exception ex)
                { 

                }
            }
            if(Request["currentpage"]!=null && Request["currentpage"].ToString()!="")
            {
                currentpage = int.Parse(Request["currentpage"].ToString());
            }
        }
        protected void BindCategory()
        {            
            string cat_type = "category";
            if(Request.Params["cat_type"] != null){
                cat_type = Request.Params["cat_type"].ToString();
            }            
            IList<OCategory> lstCategory = _BCategory.Get(catparent, lang, "CatID", "ASC", currentpage, pagesize,cat_type);
            grvCategory.DataSource = lstCategory;
            grvCategory.DataBind();
            if (lstCategory.Count > 0)
            {                
                ctlPagging.PageSize = pagesize;
                ctlPagging.ItemCount = (int)lstCategory[0].TotalResult;
                string url = "lang=" + lang;
                if (catparent != 0) { url += "&catparent=" + catparent; }                
                ctlPagging.QueryStringParameterName = url;
                ctlPagging.CurrentIndex = currentpage;
            }
        }        
        /// <summary>
        /// Xóa chuyên mục
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvCategory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int CatID = int.Parse(e.CommandArgument.ToString());
                if (_BCategory.Delete(CatID))
                {
                    lang = ddlLanguageFilter.SelectedValue;
                    catparent = int.Parse(ucddlcCategoryFilter.SelectedValue);
                    BindCategory();
                }
                else
                {
                    RegisterClientScriptBlock("NOTE","<script>alert('Chuyên mục còn bài viết không thể xóa !');</script>");
                }    
            }            
        }
        protected void lbtFunction_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "BunkFunction")
            {
                bool flag = true;
                string FunctionName = "delete";                
                switch (FunctionName)
                {
                    case "delete":
                        foreach (GridViewRow gvr in grvCategory.Rows)
                        {
                            if (((CheckBox)gvr.Cells[0].Controls[0]).Checked)
                            {
                                if (!_BCategory.Delete(int.Parse(grvCategory.DataKeys[gvr.RowIndex].Value.ToString())))
                                {
                                    flag = false;                                    
                                }
                            }
                        }
                        BindCategory();
                        break;
                }
                if (flag == false)
                {
                    RegisterClientScriptBlock("NOTE", "<script>alert('Chuyên mục còn bài viết không thể xóa !');</script>");
                }
            }
        }
        protected void grvCategory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            AddCheckBoxAll(e);
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Control control = e.Row.Cells[3].FindControl("ddlcParent");
                if (control != null)
                {
                    ((DropDowListCategory)control).SelectedValue = ((OCategory)(e.Row.DataItem)).CatParent.ToString();
                    ((DropDowListCategory)control).DataBind();
                }
            }
            BoundFunctionGridview(e);    
        }
        protected void lbtFilter_Click(object sender, EventArgs e)
        {
            if (int.Parse(ucddlcCategoryFilter.SelectedValue) != 0)
            {
                catparent = int.Parse(ucddlcCategoryFilter.SelectedValue);
                lang = ddlLanguageFilter.SelectedValue;
                BindCategory();
            }
        }

        protected void grvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
        }
    }
}
